package kayao.client.scene.objects;

import kayao.common.KayaoValues;

import com.chopsticks3d.scene.Light;
import com.chopsticks3d.scene.LightNode;
import com.chopsticks3d.scene.SceneNode;

public class KayaoWorld extends SceneNode {
	private final static int ROW_LENGTH_ROADS_MAP = 7;
	private final static char[] ROADS_MAP = {
		'+', '-', '+', ' ', ' ', ' ', ' ',
		'|', ' ', '+', '-', '+', '-', '+',
		'|', ' ', '|', ' ', '|', ' ', '|',
		'|', ' ', '|', ' ', '|', ' ', '|',
		'+', '-', '+', '-', '+', '+', '+',
		' ', ' ', '|', ' ', ' ', '|', ' ',
		' ', ' ', '+', '-', '-', '+', ' ',
	};

	public KayaoWorld() {
		Light light = new Light();
		light.setAmbient(1.0f, 1.0f, 1.0f, 1.0f);
		LightNode lightNode = new LightNode(light);
		lightNode.setTranslationY(2);
		addChild(lightNode);

		// Create roads from the roads map array
		float columnLength = (float)ROADS_MAP.length / ROW_LENGTH_ROADS_MAP;
		float roadBrickWidth = (float)KayaoValues.TOTAL_MAP_WIDTH / ROW_LENGTH_ROADS_MAP;
		float roadBrickHeight = (float)KayaoValues.TOTAL_MAP_HEIGHT / columnLength;
		float y = KayaoValues.TOTAL_MAP_HEIGHT;
		System.out.println("columnLength: " + columnLength + " BrickWidth: " + roadBrickWidth + " BrickHeight: " + roadBrickHeight);
		for (int i = 0; i < columnLength; i++) {
			float x = 0;
			for (int j = 0; j < ROW_LENGTH_ROADS_MAP; j++) {
				RoadNode road = null;
				if (ROADS_MAP[i * ROW_LENGTH_ROADS_MAP + j] == '-') {
					road = new RoadNode(RoadNode.ROAD_HORIZONTAL);
				} else if(ROADS_MAP[i * ROW_LENGTH_ROADS_MAP + j] == '|') {
					road = new RoadNode(RoadNode.ROAD_VERTICAL);
				} else if(ROADS_MAP[i * ROW_LENGTH_ROADS_MAP + j] == '+') {
					road = new RoadNode(RoadNode.ROAD_CROSSING);
				} else {
					road = new RoadNode(RoadNode.ROAD_EMPTY);
				}
				road.setTranslation(x - KayaoValues.TOTAL_MAP_WIDTH / 2 + roadBrickWidth / 2, 0,
						y - KayaoValues.TOTAL_MAP_HEIGHT / 2 - roadBrickHeight / 2);
				road.setScale(roadBrickWidth, 1, roadBrickHeight);
				addChild(road);

				x += roadBrickWidth;
			}
			y -= roadBrickHeight;
		}

		addBuilding(0, 14.5f);
		addBuilding(0, -1);
		addBuilding(0, -55);
		addBuilding(14, -55);
		addBuilding(28, -55);
		addBuilding(42, -41);
		addBuilding(42, -27);
		addBuilding(56, -13);
		addBuilding(56, 1);
		addBuilding(56, 15);
		addBuilding(56, 29);

		//top
		addBuilding(42, 43);
		addBuilding(28, 43);
		addBuilding(14, 43);
		addBuilding(0, 43);
		addBuilding(-14, 57);
		addBuilding(-28, 57);
		addBuilding(-42, 57);

		//left
		addBuilding(-56, 43);
		addBuilding(-56, 29);
		addBuilding(-56, 15);
		addBuilding(-56, 1);
		addBuilding(-56, -13);
		addBuilding(-42, -27);
		addBuilding(-28, -27);
		addBuilding(-28, -41);
		addBuilding(-14, -55);
	}

	private void addBuilding(float x, float z) {
		BuildingNode building = new BuildingNode();
		building.setTranslation(x, 0, z);

		addChild(building);
	}
}
